﻿using System;
using Microsoft.Win32;

 class Registry_Cleaner
{
    // 删除某个软件在 HKEY_CLASSES_ROOT 下注册的右键菜单（背景/目录）
    // HKEY_CLASSES_ROOT\Directory\Background\shell
    // HKEY_CLASSES_ROOT\Directory\shell
    public static int DeleteAppShellKeys(string appName)
    {
        string[] paths =
        {
            $@"Directory\Background\shell\{appName}",
            $@"Directory\shell\{appName}"
        };

        return Delete_Classes_RootKeys(paths);
    }

    private static int Delete_Classes_RootKeys(params string[] paths)
    {
        int deletedCount = 0;

        foreach (var path in paths)
        {
            try
            {
                string parentPath = System.IO.Path.GetDirectoryName(path);
                string subKeyName = System.IO.Path.GetFileName(path);

                using (RegistryKey parentKey = Registry.ClassesRoot.OpenSubKey(parentPath, writable: true))
                {
                    if (parentKey == null)
                    {
                        Console.WriteLine($"未找到父路径: {parentPath}");
                        continue;
                    }

                    using (RegistryKey targetKey = parentKey.OpenSubKey(subKeyName))
                    {
                        if (targetKey == null)
                        {
                            Console.WriteLine($"子键不存在: {path}");
                        }
                        else
                        {
                            parentKey.DeleteSubKeyTree(subKeyName);
                            Console.WriteLine($"已删除注册表项: {path}");
                            deletedCount++;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"删除 {path} 失败: {ex.Message}");
            }
        }

        return deletedCount;
    }
}


 class Delete
 {
     public static void run(string appName) 
     {
         Console.WriteLine($"------------------------------------  开始清理 {appName} 的注册表项  ----------------------------------------");
         int result = Registry_Cleaner.DeleteAppShellKeys(appName);
         Console.WriteLine($"清理完成，共删除 {result} 个注册表项。");
     }
 }
